package fr.mairie.parc.page.attribution;

import javax.annotation.Resource;

import org.apache.click.Control;
import org.apache.click.control.FieldSet;
import org.apache.click.control.ImageSubmit;
import org.apache.click.extras.control.TabbedForm;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.control.demande.DemandeFields;
import fr.mairie.parc.control.materiel.MaterielFields;
import fr.mairie.parc.control.materiel.MaterielTable;
import fr.mairie.parc.domain.Demande;
import fr.mairie.parc.domain.Materiel;
import fr.mairie.parc.domain.User;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.page.demande.ListDemandePageOld;
import fr.mairie.parc.page.materiel.ViewMaterielPage;
import fr.mairie.parc.service.DemandeManager;
import fr.mairie.parc.service.MaterielManager;
import fr.mairie.parc.service.UserManager;

public class AttributionPage extends BorderPage {


	private static final long serialVersionUID = 1L;
	@Resource protected MaterielManager materielManager;
	@Resource protected UserManager userManager;
	@Resource protected DemandeManager demandeManager;
	@Bindable protected TabbedForm  form = new TabbedForm ();
	protected DemandeFields demandefields = new DemandeFields(false);
	protected MaterielFields materielfields = new MaterielFields();
	protected transient Demande demande;
	@Bindable protected Integer idDemande;
	@Bindable protected Integer idUser;
	protected transient User user;
	@Bindable protected Integer idMateriel;
	protected transient Materiel materiel;
	protected FieldSet fiel;
	@Bindable protected MaterielTable resultTable = new MaterielTable("resultTable", this, ViewMaterielPage.class);
	protected Control control ;

	public AttributionPage () {

		demandefields.addAll(form);
		materielfields.addAll(form);

		form.addTabSheet(demandefields.getFieldset());

		form.add(new ImageSubmit("OK", "/assets/images/32x32/accept.png", this,"onSubmit"));
		form.add(new ImageSubmit("Cancel", "/assets/images/32x32/cancel.png",this, "onCancelClick"));
	}

	public void onRender() {
		if (idDemande < 0) {
			msg = "Error, invalid id:" + idDemande + " for Demande";
			setRedirect("error.htm");
			return;
		}
		if (demande == null) {
			demande =  demandeManager.getDemandeById(idDemande);
		}
		if (demande == null) {
			msg = "Error, Demande not found for id:" + idDemande;
			setRedirect("error.htm");
			return;
		}

		demandefields.setFieldsValues(demande);
		//demandeValidation.setFieldValues(demande);
		super.onRender();
	}

	@Override
	public void onInit() {
		super.onInit();
		ApplicationContext ctx = AppContext.getApplicationContext();
    	demandeManager = (DemandeManager) ctx.getBean("demandeManager");
    	userManager = (UserManager) ctx.getBean("userManager");
    	materielManager = (MaterielManager) ctx.getBean("materielManager");
	}

	public boolean onCancelClicked() {
        setRedirect(ListDemandePageOld.class);
        return false;
    }



}
